package com.jimet.utils;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

public class FilterParams {
	
	private Hashtable<String,Vector<String>> paramsHash;
	private Vector<String>keysOrderedVect;
	private int paramSize ;
	private String page;
	private String divId;
	private String strFilterValues;
	private String lasFilterPage;
	
	/**
	 * 
	 */
	public FilterParams()
	{
		this.paramsHash 	= new Hashtable<String, Vector<String>>();
		this.keysOrderedVect= new Vector<String>();
		this.strFilterValues = new String();
		this.lasFilterPage	 = new String();
		
		this.paramSize	 	= 0;
		this.page = "";
		this.divId = "";
	}
	
	public String getLasFilterPage() {
		return lasFilterPage;
	}

	public void setLasFilterPage(String lasFilterPage) {
		this.lasFilterPage = lasFilterPage;
	}

	public String getStrFilterValues() {
		return strFilterValues;
	}

	public void setStrFilterValues(String strFilterValues) {
		this.strFilterValues = strFilterValues;
	}

	public void addParam( String labelName , String paramName, String paramValue , String paramText , String type , String operator)
	{
		Vector<String> paramsVec = new Vector<String>();
		paramsVec.add(labelName);
		paramsVec.add(type);
		paramsVec.add(operator);
		paramsVec.add(paramValue);
		paramsVec.add(paramText);
		paramsVec.add("0");
		
		this.paramsHash.put(paramName, paramsVec);
		this.keysOrderedVect.add(paramName);
		this.paramSize = this.paramsHash.size();
		
	}
	
	public void addParam( String labelName , String paramName, String paramValue , String paramText , String type , String operator,boolean isJoin)
	{
		Vector paramsVec = new Vector();
		paramsVec.add(labelName);
		paramsVec.add(type);
		paramsVec.add(operator);
		paramsVec.add(paramValue);
		paramsVec.add(paramText);

		if( isJoin )
			paramsVec.add("1");
		else
			paramsVec.add("0");
		
		this.paramsHash.put(paramName, paramsVec);
		this.keysOrderedVect.add(paramName);
		this.paramSize = this.paramsHash.size();
		
	}
	
	public Vector<String> getParamValue( String paramName )
	{
		return this.paramsHash.get(paramName);
	}
	
	public String getParamStrValue( String paramName )
	{
		return (String) this.paramsHash.get(paramName).get(3);
	}
	
	
	
	public Hashtable<String,Vector<String>> getFilterParams()
	{
		return this.paramsHash;
	}
	
	public void removeParam( String paramName )
	{
		this.paramsHash.remove(paramName);
		this.paramSize = this.paramsHash.size();
		this.keysOrderedVect.removeElement(paramName);
	}
	
	public int getParamsSize()
	{
		return this.paramSize;
	}

	public String getPage() {
		return page;
	}
	
	
	public String getDivId() {
		return divId;
	}

	public void setPage(String page, String divId) {
		this.page = page;
		this.divId = divId;
		this.paramsHash 		= new Hashtable<String, Vector<String>>();
		this.keysOrderedVect	= new Vector<String>();
	}
	
	public void overrideParamValue( String paramName , String newValue )
	{
		Vector<String> paramsVec =  this.getParamValue(paramName);
		paramsVec.set(3, newValue);
	}
	
	public void overrideParamOperator( String paramName , String operator )
	{
		Vector<String> paramsVec =  this.getParamValue(paramName);
		paramsVec.set(2, operator);
	}
	
	public void overrideParamText( String paramName , String text )
	{
		Vector<String> paramsVec =  this.getParamValue(paramName);
		paramsVec.set(4, text);
	}
	
	public Vector<Vector<String>> getStandartFieldsWithExprVector()
	{
		Vector<Vector<String>> resultVec 		= new Vector<Vector<String>>(); 
		Vector<String> pagingVec 		= new Vector<String>();
		Vector<String> pagingVecJoin 	= new Vector<String>();
		int paramSize = getParamsSize();
		if (paramSize > 0) {
			Hashtable parametersHash = getFilterParams();
			Enumeration en = parametersHash.keys();

			while (en.hasMoreElements()) {
				String paramKey = (String) en.nextElement();
				Vector paramVec = (Vector) parametersHash.get(paramKey);
				String type = (String) paramVec.get(1);
				String op = (String) paramVec.get(2);
				String val = (String) paramVec.get(3);
				String isJoin = (String) paramVec.get(5);
				if (val.equals("-1") || val.equals(""))
					continue;

				if (type.equals("text") || type.equals("date")) {
					if (op.equals("startsWith")) {
						op = "LIKE";
						val = "'" + val + "%'";
					} else if (op.equals("endsWith")) {
						op = "LIKE";
						val = "'%" + val + "'";
					} else if (op.equals("LIKE"))
						val = "'%" + val + "%'";
					else
						val = "'" + val + "'";
				}

				if (isJoin.equals("0")) {
					pagingVec.add(paramKey);
					pagingVec.add(op + val);
				} else if (isJoin.equals("1")) {
					pagingVecJoin.add(paramKey);
					pagingVecJoin.add(op + val);
				}

			}
		}
		
		resultVec.add(pagingVec);
		resultVec.add(pagingVecJoin);
		
		return resultVec;
		
	}

	public Vector<String> getKeysOrderedVect() {
		return keysOrderedVect;
	}
	
	public boolean hasKeyLike( String searchKey )
	{
		if( this.keysOrderedVect == null )
			return false;
		
		return this.keysOrderedVect.contains(searchKey);
	}

	
}
